<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="tableDataList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="预警天数" align="center" prop="warnDay" :show-overflow-tooltip="true" />
      <el-table-column label="提醒人" align="center" prop="userNames" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPerm="['sys_config_edit_brand_warn']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="预警天数" prop="warnDay">
          <el-input-number v-model="form.warnDay" :min="1" :max="10" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="通知人" prop="noticUserIds">
          <el-transfer
            :titles="titles"
            filterable
            :filter-method="filterMethod"
            filter-placeholder="请选择通知人"
            :props="{key: 'id',label: 'nickName'}"
            v-model="form.noticUserIds"
            :data="userList">
          </el-transfer>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPerm="['sys_config_edit_brand_warn']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { editSysConfig, getSysConfigById } from '@/api/admin/config'
  import { listAllUser } from '@/api/system/user'

  export default {
  name: "AdminConfigWarnBrand",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 配置对象
      config: {},
      titles: ['候选人','通知人'],
      //用户列表
      userList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 品牌表格数据
      tableDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
        key: undefined,
        isSys: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticUserIds: [
          { required: true, message: "通知人至少要选择一位", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    getUserList() {
      this.loading = true
      listAllUser().then(response => {
        this.loading = false
        this.userList = response.data;
        this.getList();
      });
    },
    filterMethod(query, item) {
      return item.nickName.indexOf(query) > -1;
    },
    /** 获取配置信息 */
    getList() {
      this.loading = true
      getSysConfigById(1).then(response => {
          this.config = response.data
          this.tableDataList=JSON.parse(response.data.configItemValue);
          for (let i in this.tableDataList) {
            let userNames = []
            let row=this.tableDataList[i];
            for (const k in row.noticUserIds) {
              let userId=row.noticUserIds[k];
              for (const j in this.userList) {
                let user=this.userList[j];
                if (userId == user.id) {
                  userNames.push(user.nickName)
                  break
                }
              }
            }
            row.userNames = userNames.join('|');
          }
          this.total = this.tableDataList.length || 0
          this.loading = false
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warnDay: undefined
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.reset();
      // this.getUserList();
      const id = row.id;
      this.open = true;
      this.title = "修改预警信息";
      this.form=row;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.loading = true
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            this.tableDataList.forEach(item=>delete item.userNames);
            this.config.configItemValue=JSON.stringify(this.tableDataList);

            editSysConfig(this.config).then(response => {
              this.loading = false
              if (response.code === 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            this.msgError('数据异常,无法保存');
          }
        }else {
          this.loading = false
        }
      });
    }
  }
};
</script>
